import hashlib
import json
import time
import execjs
import requests
from urllib import parse
from pyecharts.charts import Bar, Line, Grid
from pyecharts import options as opts

print(parse.quote('成都'))

time_stamp = int(time.time() * 1000)
print(time_stamp)


def get_token():
    token = "path" + "/api/pricetrend/getChartMultiCity.htm" + "timestamp" + f"{time_stamp}" + "version1.0.0" + "a57f694a4dae4918"
    return hashlib.md5(token.encode()).hexdigest().upper()


print(get_token())

headers = {
    "Referer": "https://index.mysteel.com/price/indexPrice.html",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36",
}

url = "https://index.mysteel.com/api/pricetrend/getChartMultiCity.htm"
params = {
    "catalog": "%E6%97%A0%E7%BC%9D%E7%AE%A1_:_%E6%97%A0%E7%BC%9D%E7%AE%A1",
    "city": "%E5%8D%97%E5%AE%81,%E6%B2%88%E9%98%B3,%E9%95%BF%E6%B2%99,%E6%AD%A6%E6%B1%89,%E5%8C%97%E4%BA%AC,%E5%8D%97%E4%BA%AC",
    "spec": "%CE%A6219*6_:__%CE%A6219*6",
    "startTime": "2024-11-04",
    "endTime": "2024-11-14",
    "callback": "json",
    "v": "1731901853916",
    "Api-Token": "24BDAF43D4FE9396EA5113682E36F5BE",
    "Timestamp": "1731892837922"
}

response = requests.get(url, headers=headers, params=params)
with open("数据解析.js", "r", encoding="utf-8") as f:
    content = f.read()
    js_code = execjs.compile(content)
    print(js_code.call("decryptEncryptFields", response.json(), 'data', True))
    datas = js_code.call("decryptEncryptFields", response.json(), 'data', True)
    bar = Line()
    detail_time = []
    for data in datas:
        print(data)
        detail_value = []
        for detail_data in data['dateValueMap']:
            if detail_data['date'] not in detail_time:
                detail_time.append(detail_data['date'])
        bar.add_xaxis(detail_time)
        for detail_data in data['dateValueMap']:
            detail_value.append(detail_data['value'])
        bar.add_yaxis(data['lineName'], detail_value)

    bar.set_global_opts(title_opts=opts.TitleOpts(title="某商场销售情况"))
    bar.render(path="shiyan.html")
